package Thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account {
    private String cardid;
    private double money;
    private final Lock lock=new ReentrantLock();
    public Account() {
    }

//    public synchronized void drawMoney(double money){
    public void drawMoney(double money){
         String name=Thread.currentThread().getName();
//        synchronized (this) {
            lock.lock();
            try{
                if (this.money>=money)
            {
                System.out.println(name+"取钱成功"+money);
                this.money=this.money-money;
                System.out.println(name+"取钱后剩余"+this.money);
            }else {
                System.out.println(name+"取钱，余额不足");
            }
        }
            finally {
                lock.unlock();
            }

//        }

    }

    public Account(String cardid, double money) {
        this.cardid = cardid;
        this.money = money;
    }

    public String getCardid() {
        return cardid;
    }

    public void setCardid(String cardid) {
        this.cardid = cardid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}
